#include <Arduino.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ADC
const float VoltageDividerRatio = 8.4; // 电压分压比
const float MinVoltage = 6.4;
const float MaxVoltage = 8.4;
const int NumSamples = 3;
float BatteryVoltage = 0.0;
int BatteryPercentage = 0;

void GetAverageAdcVoltage() {
  long totalAdcValue = 0;
  for(int i = 0; i < NumSamples; i++) {
    totalAdcValue += analogRead(A0);
    delay(10);
  }
  float averageAdcValue = totalAdcValue / (float)NumSamples;
  BatteryVoltage = averageAdcValue / 1023.0 * VoltageDividerRatio;
  if(BatteryVoltage <= MinVoltage) {
    BatteryPercentage = 0;
  } else if(BatteryVoltage >= MaxVoltage) {
    BatteryPercentage = 100;
  } else {
    BatteryPercentage = (int)((BatteryVoltage - MinVoltage) / (MaxVoltage - MinVoltage) * 100);
  }
}